package me.firstapp.repository.bbs.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.stereotype.Repository;

import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.module.bbs.Reply;
import me.firstapp.repository.base.impl.EntityRepositoryImpl;
import me.firstapp.repository.bbs.ReplyRepository;

@Repository
public class ReplyRepositoryImpl extends EntityRepositoryImpl<Reply> implements ReplyRepository {

	@Override
	public Long findTopicReplyCounts(Long topicId, Long apiClientId) {
		StringBuilder sql = new StringBuilder("SELECT COUNT(bean.id) FROM Reply bean WHERE 1=1 ");
		Map<String, Object> parameters = new HashMap<String, Object>();

		sql.append(" AND bean.apiClientId =:apiClientId ");
		parameters.put("apiClientId", apiClientId);

		sql.append(" AND bean.topicId = :topicId");
		parameters.put("topicId", topicId);

		List<Long> result = super.query(Long.class, sql.toString(), parameters);
		return result.isEmpty() ? 0 : result.get(0);
	}

	@Override
	protected Class<Reply> getPersistentClass() {
		return Reply.class;
	}

	@Override
	public Page<Reply> findRepliesPage(Integer pageNo, Integer pageSize, Long topicId, Long fromMemberId,
			Long toMemberId, Long apiClientId) {
		StringBuilder sql = new StringBuilder(" FROM Reply bean WHERE 1=1 ");
		Map<String, Object> parameters = new HashMap<String, Object>();

		sql.append(" AND bean.apiClientId =:apiClientId ");
		parameters.put("apiClientId", apiClientId);

		if (topicId != null) {
			sql.append(" AND bean.topicId=:topicId ");
			parameters.put("topicId", topicId);
		}
		if (fromMemberId != null) {
			sql.append(" AND bean.fromMember.id=:fromMemberId ");
			parameters.put("fromMemberId", fromMemberId);
		}
		if (toMemberId != null) {
			sql.append(" AND bean.toMember.id=:toMemberId ");
			parameters.put("toMemberId", toMemberId);
		}
		sql.append(" ORDER BY bean.createTime ASC");
		return super.queryForPage(Reply.class, sql.toString(), parameters, pageNo, pageSize);
	}

	@Override
	public Page<Reply> findRepliesPageByParams(Long topicId, Integer pageNo, Integer pageSize,
			Map<String, Object> searchParams, String sortParam, String sortType, Long apiClientId) {
		StringBuilder sql = new StringBuilder(" FROM Reply bean WHERE 1=1 ");
		Map<String, Object> parameters = new HashMap<String, Object>();

		if (topicId != null) {
			sql.append(" AND bean.topicId=:topicId ");
			parameters.put("topicId", topicId);
		}
		if (searchParams != null) {
			Set<String> keys = searchParams.keySet();
			for (String key : keys) {
				Object value = searchParams.get(key);
				if (value != null) {
					sql.append(" AND bean." + key + " LIKE :value ");
					parameters.put("value", "%" + value + "%");
				}
			}
		}
		sql.append(" AND bean.apiClientId =:apiClientId ");
		parameters.put("apiClientId", apiClientId);
		if (StrUtils.isNotNULL(sortParam) && StrUtils.isNotNULL(sortType)) {
			sql.append(" ORDER BY " + sortParam + " " + sortType);
		}

		return super.queryForPage(Reply.class, sql.toString(), parameters, pageNo, pageSize);
	}

	@Override
	public Long findReplyCountsBySectionId(Long sectionId, Long apiClientId) {
		StringBuilder sql = new StringBuilder("SELECT COUNT(*) FROM Reply rep WHERE 1=1 ");
		Map<String, Object> parameters = new HashMap<String, Object>();

		sql.append(" AND rep.apiClientId =:apiClientId ");
		parameters.put("apiClientId", apiClientId);

		sql.append(" AND rep.topicId IN(SELECT top.id FROM Topic top WHERE top.section.id =:sectionId)");
		parameters.put("sectionId", sectionId);

		List<Long> result = super.query(Long.class, sql.toString(), parameters);
		return result.isEmpty() ? 0 : result.get(0);
	}

	@Override
	public Reply findReplyById(Long id, Long apiClientId) {
		StringBuilder sql = new StringBuilder(" FROM Reply bean WHERE 1=1 ");
		Map<String, Object> parameters = new HashMap<String, Object>();

		sql.append(" AND bean.apiClientId =:apiClientId ");
		parameters.put("apiClientId", apiClientId);

		sql.append(" AND bean.id =:id ");
		parameters.put("id", id);

		List<Reply> result = super.query(Reply.class, sql.toString(), parameters);
		return result.isEmpty() ? null : result.get(0);
	}

}
